package com.wowangz.cms.syscore.autotask.job;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.reflect.TypeToken;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.dailyoperation.chargemanagenew.pojo.Charge;
import com.wowangz.cms.dailyoperation.services.pojo.Services;
import com.wowangz.cms.dailyoperation.services.pojo.ServicesMenu;
import com.wowangz.cms.dailyoperation.storechargemanage.pojo.StoreChargeManage;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.autotask.service.ServiceSyncService;
import com.wowangz.cms.syscore.utils.JedisUtil;

public class ServiceSync extends BaseJob{

	private static Logger logger = LoggerFactory.getLogger(ServiceSync.class);
	
	/**
	 * 同步所有菜单信息至redis
	 */
	@Override
	public void innerExecute(JobExecutionContext context) throws Exception
	{
		logger.info("进入服务菜单自动任务");
		try
		{
			//查询所有菜单信息同步至redis
			//存的是复核的有价格的服务信息
			//edit by yoyo 同步所有启用的服务
			Charge charge = new Charge();
			charge.setStatusid(SysConstant.RecordStatus.VALID);
			charge.setCheckstatus(SysConstant.CheckStatus.CHECKED);
			charge.setBusinessstatus(SysConstant.CommonUsedStatus.START);
			
			ServiceSyncService serviceSyncService = (ServiceSyncService) IssTransactionContainer.getInstance(ServiceSyncService.class, false);
			List<Charge> list = serviceSyncService.queryChargeList(charge);
			Map map = new HashMap();
			Set<Long> menuidSet = null;
			for (Charge tmp : list)
			{
				menuidSet = new HashSet<Long>();
				tmp.setStatusid(SysConstant.RecordStatus.VALID);
				List<ServicesMenu> serviceMenuList = serviceSyncService.queryServiceMenuList(tmp);
				for (ServicesMenu servicesMenu : serviceMenuList)
				{
					menuidSet.add(servicesMenu.getMenuid());
				}
				tmp.setMenuidset(menuidSet);
				map.put(tmp.getServiceid(), tmp);
			}
			JedisUtil.insertPublicDataObject(JedisUtil.BUY_SERVICE_KEY, map,  JedisUtil.SERVICE);
			
			Type type = new TypeToken<HashMap<Long,Charge>>() {}.getType();
			HashMap hashmap = (HashMap)JedisUtil.getPublicDataObjectByType(JedisUtil.BUY_SERVICE_KEY, type, JedisUtil.SERVICE);
			System.out.println("redis中service信息：" + hashmap.keySet());
			
			//查询商户特殊服务,特殊的商户会对服务定义特殊的价格
			//以商户ID为key
			List<StoreChargeManage> storeChargeManageList = serviceSyncService.queryStoreChargeManageList(charge);
			Map<Long,List<StoreChargeManage>> chargeMap = new HashMap<Long,List<StoreChargeManage>>();
			for (StoreChargeManage storeChargeManage : storeChargeManageList)
			{
				if(chargeMap.get(storeChargeManage.getStoreid()) != null)
				{
					List<StoreChargeManage> storelist = chargeMap.get(storeChargeManage.getStoreid());
					storelist.add(storeChargeManage);
				}else
				{
					List<StoreChargeManage> storelist = new ArrayList<StoreChargeManage>();
					storelist.add(storeChargeManage);
					chargeMap.put(storeChargeManage.getStoreid(), storelist);
				}
			}
			JedisUtil.insertPublicDataObject(JedisUtil.STORE_SERVICE_KEY, chargeMap,  JedisUtil.SERVICE);
			
			Type type2 = new TypeToken<HashMap<Long,List<StoreChargeManage>>>() {}.getType();
			HashMap hashmap2 = (HashMap)JedisUtil.getPublicDataObjectByType(JedisUtil.STORE_SERVICE_KEY, type2, JedisUtil.SERVICE);
			System.out.println("redis中service信息：" + hashmap2.keySet());
			
			
			//以serviceid为key，存service所有的菜单ID
			Services services = new Services();
			services.setStatusid(SysConstant.RecordStatus.VALID);
			List<Services> serviceList = serviceSyncService.queryServiceList(services);
			Map serviceMap = new HashMap();
			for (Services tmp : serviceList)
			{
				menuidSet = new HashSet<Long>();
				charge = new Charge();
				charge.setStatusid(SysConstant.RecordStatus.VALID);
				charge.setServiceid(tmp.getId());
				List<ServicesMenu> serviceMenuList = serviceSyncService.queryServiceMenuList(charge);
				for (ServicesMenu servicesMenu : serviceMenuList)
				{
					menuidSet.add(servicesMenu.getMenuid());
				}
				serviceMap.put(tmp.getId(), menuidSet);
			}
			
			JedisUtil.insertPublicDataObject(JedisUtil.ALL_SERVICE_KEY, serviceMap,  JedisUtil.SERVICE);
			
			Type type3 = new TypeToken<HashMap<Long,List<Long>>>() {}.getType();
			HashMap hashmap3 = (HashMap)JedisUtil.getPublicDataObjectByType(JedisUtil.ALL_SERVICE_KEY, type3, JedisUtil.SERVICE);
			System.out.println("redis中service所对应的菜单ID：" + hashmap3);

		}catch(Exception e)
		{
			e.printStackTrace();
		}
		logger.info("同步服务自动任务运行结束");
	}

}
